package viewOfBst;

import javafx.application.Application;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextField;
import javafx.scene.layout.BorderPane;
import javafx.scene.layout.HBox;
import javafx.stage.Stage;
import bst.*;
public class BSTAnimation extends Application {

	@Override
	public void start(Stage stage ) throws Exception {
		
		BST<Integer> tree=new BST<>();
		BorderPane pane= new BorderPane();
		BTView view=new BTView(tree);
		pane.setCenter(view);
		
		TextField tfKey=new TextField();
		
		tfKey.setPrefColumnCount(3);
		Button btInsert = new Button("Insert");
		Button btDelete = new Button("delete");
		HBox hbox=new HBox();
		hbox.getChildren().addAll(new Label("Enter a key: "),tfKey,btInsert,btDelete);
		hbox.setAlignment(Pos.CENTER);
		pane.setBottom(hbox);
		
		btInsert.setOnAction(e->{
			
			int key=Integer.parseInt(tfKey.getText());
			if(tree.search(key)) {
				view.displayTree();
				view.setStatus(key+" is already in the tree");
			}
			else {
				tree.insert(key);
				view.displayTree();
				view.setStatus(key+" is inserted in the tree");
			}
		});
		
		btDelete.setOnAction(e->{
			int key=Integer.parseInt(tfKey.getText());
			if(tree.search(key)) {
				tree.delete(key);
				view.displayTree();
				view.setStatus(key+" is deleted from the tree");
			}
			else {
				
				view.displayTree();
				view.setStatus(key+" is not in the tree");
			}
			
		});
		
		Scene scene=new Scene(pane,450,250);
		stage.setScene(scene);
		stage.show();
		
		
	}
	public static void main(String[] args) {
		Application.launch(args);
		
		

	}
}
